<template>
	<view class="main">
		<!-- <view class="link">登录</view> -->
		<image class="login-img" src="../../static/images/login_02.png" mode=""></image>
		<view class="yanzhengma">
			<view class="yzm-mima">
				<view class="yazm">
					<view>{{isshow?'验证码登录':'密码登录'}}</view>
				</view>
				<view class="mima">
					<view @click="isshow=!isshow">{{isshow?'密码登录':'验证码登录'}}</view>
				</view>
			</view>
			<view class="shurukuang" v-show="isshow">
				<view class="shoujihao">
					<input class="input-sjh" type="text" placeholder="请输入手机号" v-model="form.mobile" maxlength="11" minlength="11">
				</view>
				<view class="yamzma">
					<input class="input-sjh" type="text" placeholder="请输入验证码" v-model="form.code">
					<text class="huoqu" @click="sendCode" v-show="second==60">获取验证码</text>
					<text class="huoqu" @click="sendCode" v-show="second!=60">{{second}}</text>
				</view>
			</view>
			<view class="shurukuang" v-show="!isshow">
					<view class="shoujihao">
						<input class="input-sjh" type="text" placeholder="请输入账号" v-model="passform.mobile">
					</view>
					<view class="yamzma">
						<input class="input-sjh" type="text" placeholder="请输入密码" v-model="passform.pass">
					</view>
			</view>

		</view>
		<view class="denglu" @click="login(form)">登录</view>	
		<view class="meiyouzhanghao" @click="meiyou">没有账号，立即注册</view>
	</view>
</template>

<script>
	import Index from '../../apis/index.js'
	import {mapMutations} from 'vuex'
	import {debounce} from '../../untils/global.js'
	export default {
		data() {
			return {
				isshow:true,
				form:{
					mobile:'15098231959',
					code:'123456'
				},
				passform:{
					mobile:'15098231959',
					pass:'123456'
					
				},
				second:60
			}
		},
		onLoad(){
		},
		methods: {
			...mapMutations(['setBuilding']),
			//没有账号，立即注册
			meiyou(){
				uni.navigateTo({
				url: '/pages/register/register',
				})
			},
			// islogin(){
			// 	debounce(this.login,300)
			// },
			async sendCode(){
				let self = this
				let reg = /^1[3-9]\d{9}$/
				if(!reg.test(this.form.mobile)){
					this.$message.error('请输入正确手机号')
					return
				}
				let res = await Index.sendcode({'mobile':this.form.mobile})
				// console.log(res);
				if(res.errCode==10000){
					//倒计时
					let timer =  setInterval(()=>{
						self.second--
						if(self.second==0){
							clearInterval(timer)
							self.second = 60
						}
					},1000)
					this.$message.none('发送成功')
				}else{
					this.$message.error(res.errMsg)
				}
			},
			async login(form){
				if(this.isshow){
					//验证码登录
					let reg = /^1[3-9]\d{9}$/
					if(!reg.test(this.form.mobile)){
						this.$message.error('请输入正确手机号')
						return
					}
					let codereg = /^\d{6}$/
					if(!codereg.test(this.form.code)){
						this.$message.error('请输入正确验证码')
						return
					}
					 let res =  await Index.loginByCode(this.form)
					if(res.errCode==10000){
						uni.setStorageSync('token',res.data.token)
						uni.setStorageSync('member',res.data.member)
						this.setBuilding(res.data.buildings)
						//buildingState ==0 跳转到房屋认证界面
						//buildingState ==1 buildingState =【】 等待审核
						//buildingState ==1 buildings,length>1  已认证
						if(res.data.buildingState ==0){
							uni.navigateTo({
								url:'/pages/HousePage/HousePage'
							})
						}
						if(res.data.buildingState ==1 && res.data.buildings.length==0){
						 	uni.navigateTo({
								url:`/pages/HouseAudit/HouseAudit?form=${encodeURIComponent(JSON.stringify(form))}`							
						 	})
						}
						if(res.data.buildingState ==1 && res.data.buildings.length>1){
						 	uni.switchTab({
								url:'/pages/index/index'
						 	})
						}
					}else{
						this.$message.error(res.errMsg)
					}
				}else{
					//密码登录
					let reg = /^1[3-9]\d{9}$/
					if(!reg.test(this.passform.mobile)){
						this.$message.error('请输入正确手机号')
						return
					}
					if(!this.passform.pass){
						this.$message.error('请输入正确密码')
						return
					}
					let res = await Index.memberloginx(this.passform)
					if(res.errCode==10000){
						uni.setStorageSync('token',res.data.token)
						uni.setStorageSync('member',res.data.member)
						this.setBuilding(res.data.buildings)
						//buildingState ==0 跳转到房屋认证界面
						//buildingState ==1 buildingState =【】 等待审核
						//buildingState ==1 buildings,length>1  已认证
						if(res.data.buildingState ==0){
							uni.navigateTo({
								url:'/pages/HousePage/HousePage'
							})
						}
						if(res.data.buildingState ==1){
						 	uni.navigateTo({
								url:'/pages/HouseAudit/HouseAudit'
						 	})
						}
						if(res.data.buildingState ==2){
						 	uni.switchTab({
								url:'/pages/index/index'
						 	})
						}
					}else{
						this.$message.error(res.errMsg)
					}
					
				}
				
			}
		}
	}
</script>

<style>
@import url('../../static/css/login/login.css');
</style>
